package ui.activity.login;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Dailog.CommomDialog;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.ItemBean;
import bean.Register2Bean;
import bean.RegisterBean;
import student.andlei.com.student.R;
import ui.activity.MainActivity;
import utils.GsonUtils;
import utils.SPUtils;
import utils.StringUtils;
import view.BottomListPopWindow;

public class RegisterNextActivity extends BaseNetActivity {
    private EditText ed_name;
    private TextView ed_grade,
            ed_class;
    private Button bt_register;
    private View layoutview;
    private List<ItemBean> list = new ArrayList<>();
    private List<RegisterBean.Grade_info> Grade_list;
    private List<RegisterBean.School_info> School_list;
    private int student_id;
    private String  invcode;
    private int grade_id,schedule_id;
    private String school_status = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("注册");
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        hideLoadDialog();
        if (action.equals("common/register2")) {
            if (baseBean.code == 200) {
                Register2Bean Bean = (Register2Bean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), Register2Bean.class);
                SPUtils.getInstance(this).put("token",Bean.token);
                SPUtils.getInstance(this).put("accid",Bean.accid);
                SPUtils.getInstance(this).put("school_id",Bean.school_id);
                SPUtils.getInstance(this).put("student_id",Bean.student_id);
                if(!StringUtils.isNull(ed_class.getText().toString())){
                    SPUtils.getInstance(this).put("school_status",10);//0没有学校 10待审核 30审核失败 100审核通过
                    new CommomDialog(this, R.style.dialog, "恭喜，已注册并提交成功，等待审" +
                            "核，学校审核通过后，即可开启美好的课程学习！", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if (confirm) {
                                jumpTo(MainActivity.class);
                                dialog.dismiss();
                            }
                        }
                    }).setTitle("提示").hideNegativeButton().settPositiveButtonColor(R.color.homepage_titlefont).show();
                }else {
                    jumpTo(MainActivity.class);
                }
            }else {
                toast(baseBean.msg);
            }
        }else if(action.equals("common/getGradeAndSchool")){
            if(baseBean.code == 200){
                RegisterBean resultBean = new Gson().fromJson(baseBean.data.toString(),RegisterBean.class);
                Grade_list =  resultBean.grade_info;
                School_list = resultBean.school_info;
            }
        }

    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.activity_register_next, null);
        return R.layout.activity_register_next;
    }

    @Override
    protected void findViews() {
        ed_name = getView(R.id.ed_student_name);
        ed_grade = getView(R.id.ed_student_grade);
        ed_class = getView(R.id.ed_student_class);
        bt_register = getView(R.id.bt_register);
    }

    @Override
    protected void formatViews() {
        bt_register.setOnClickListener(this);
        ed_grade.setOnClickListener(this);
        ed_class.setOnClickListener(this);
    }

    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle(Bundle bundle) {
        Grade_list = (List<RegisterBean.Grade_info>) bundle.getSerializable("Grade_info");//获取list方式
        School_list = (List<RegisterBean.School_info>) bundle.getSerializable("School_info");//获取list方式
        student_id = bundle.getInt("student_id");
        invcode = bundle.getString("invcode");
        if (!StringUtils.isNull(bundle.getString("degree_id=0"))) {
            school_status = bundle.getInt("school_status")+"";
            //0没有学校 10待审核 30审核失败 100审核通过
            if (school_status.equals("0")) {
                ed_class.setVisibility(View.GONE);
            } else {
                ed_grade.setVisibility(View.GONE);
            }
            post("common/getGradeAndSchool",BaseBean.class,false);
        } else {
            if (!StringUtils.isNull(invcode)) {
                ed_grade.setVisibility(View.GONE);
            } else {
                ed_class.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //注册
            case R.id.bt_register:
                Map<String, String> map = new HashMap<>();
                map.put("student_id", "" + student_id);
                map.put("real_name", "" + ed_name.getText().toString());
                map.put("grade_id", "" + grade_id);
                map.put("schedule_id", "" + schedule_id);
                post("common/register2", map, Register2Bean.class, false);
                break;
            //选择年级信息
            case R.id.ed_student_grade:
                if (updateList(Grade_list).size() <= 0) {
                    toast("暂无年段信息");
                    return;
                }
                final BottomListPopWindow popWindow = new BottomListPopWindow(this, updateList(Grade_list));
                popWindow.showAtBottom(layoutview);
                popWindow.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        grade_id = list.get(position).id;

                        ed_grade.setText(list.get(position).str);
                        popWindow.dismiss();
                    }
                });
                break;
            //选择班级信息
            case R.id.ed_student_class:
                if (updateList2(School_list).size() <= 0) {
                    toast("暂无班级信息");
                    return;
                }
                final BottomListPopWindow popWindow2 = new BottomListPopWindow(this, updateList2(School_list));
                popWindow2.showAtBottom(layoutview);
                popWindow2.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        grade_id = list.get(position).id;
                        schedule_id =  list.get(position).schedule_id;
                        ed_class.setText(list.get(position).str);
                        popWindow2.dismiss();
                    }
                });
                break;
        }
    }

    private List<ItemBean> updateList(List<RegisterBean.Grade_info> mlist) {
        list.clear();
        if (mlist != null) {
            for (RegisterBean.Grade_info info : mlist) {
                ItemBean bean = new ItemBean();
                bean.setStr(info.grade_name);
                bean.setId(info.grade_id);
                list.add(bean);
            }
        }
        return list;
    }

    private List<ItemBean> updateList2(List<RegisterBean.School_info> mlist) {
        list.clear();
        if (mlist != null) {
            for (RegisterBean.School_info info : mlist) {

                    ItemBean bean = new ItemBean();
                    bean.setStr(info.schedule_name);
                    bean.setId(info.grade_id);
                    bean.setSchedule_id(info.schedule_id);
                    list.add(bean);

            }
        }
        return list;
    }
}
